/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.service.syndication;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;

import net.martinimix.domain.syndication.SyndicationFeed;
import net.martinimix.domain.syndication.SyndicationFeedEntry;
import net.martinimix.service.product.ProductService;

/**
 * Provides a product attribute substituting syndication feed
 * publishing service.
 * 
 * The service relies on another <code>SyndicationFeedPublishingService</code>
 * to actually perform publication.  The only value of this class is to perform
 * attribute substitutions on a feed's entries prior to publication.
 * 
 * @author Scott Rossillo
 *
 */
public class ProductAttributeSubstitutingFeedPublishingService 
		implements SyndicationFeedPublishingService {

	protected SyndicationFeedPublishingService syndicationFeedPublishingService;
	protected ProductService productService;
	
	/**
	 * Creates a new product attribute feed publishing service.
	 */
	public ProductAttributeSubstitutingFeedPublishingService() { }
	
	public String getFeedBaseUrl() {
		return syndicationFeedPublishingService.getFeedBaseUrl();
	}

	/**
	 * Updates attributes on any product based feed entries before
	 * publishing the feed.
	 * 
	 * @see #updateAttributes(SyndicationFeed)
	 * @see #setSyndicationFeedPublishingService(SyndicationFeedPublishingService)
	 */
	public final void publish(SyndicationFeed feed)
			throws SyndicationFeedPublishFailedException {
	
		updateAttributes(feed);
		syndicationFeedPublishingService.publish(feed);
	}
	
	
	/**
	 * Updates attributes on the given feed.
	 * 
	 * @param feed
	 */
	protected void updateAttributes(SyndicationFeed feed) {
		
		List entries = feed.getEntries();
		SyndicationFeedEntry entry;
		
		for(int i = 0; i < entries.size(); i++) {
			entry = (SyndicationFeedEntry) entries.get(i);
			updateAttributes(entry);
		}
	}
	
	/**
	 * Updates attributes on the given entry.
	 * 
	 * @param entry
	 */
	protected void updateAttributes(SyndicationFeedEntry entry) {
		
		// TODO: always assign US currency?
		NumberFormat format = DecimalFormat.getCurrencyInstance(Locale.US);
		BigDecimal price;
		
		if(entry.getProductId() == null) {
			return;
		}
		
		price = productService.getProductPrice(entry.getProductId());
		
		if(entry.getTitle() != null) {
			entry.setTitle(replacePrice(entry.getTitle(), format.format(price)));
		}
		
		if(entry.getSummary() != null) {
			entry.setSummary(replacePrice(entry.getSummary(), format.format(price)));
		}
		
	}
	
	public String replacePrice(String text, String price) {
		
		// FIXME: this is an ugly hack to stop $ from regexp interp
		text = text.replaceAll("\\$\\{salePrice\\}", price.replace('$', 'q'));
		return text.replace('q', '$');
	}

	/**
	 * Sets the syndication feed publishing service this service will rely
	 * on to actually perform publication.
	 * 
	 * @param syndicationFeedPublishingService the
	 * <code>SyndicationFeedPublishingService</code> this service will use
	 * to publish <code>SyndicationFeed</code>s
	 */
	public void setSyndicationFeedPublishingService(
			SyndicationFeedPublishingService syndicationFeedPublishingService) {
		this.syndicationFeedPublishingService = syndicationFeedPublishingService;
	}

	/**
	 * Sets the product service for this feed publishing service.
	 *  
	 * @param productService the <code>ProductService</code> to use when
	 * retrieving product details
	 */
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	
	

}
